package com.jr9z.jrjz.controller;

import com.github.pagehelper.PageHelper;
import com.jr9z.jrjz.common.BaseResponse;
import com.jr9z.jrjz.common.ResponseBuilder;
import com.jr9z.jrjz.entity.TeamMember;
import com.jr9z.jrjz.service.TeamMemberService;
import com.jr9z.jrjz.utils.Constant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @Author: MrZcc
 * @Description:团队管理controller
 * @Date: Created in 17:12 2019-3-26
 * @Modified By:
 */

@Controller
@RequestMapping("/team")
public class TeamMemberController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private TeamMemberService teamMemberService;


    /**
     * 跳转到团队管理
     *
     * @return
     */
    @RequestMapping("/teamManage")
    public String teamManage() {
        logger.info("进入团队管理");

        return "/team/teamManage";

    }

    /**
     * @Description:查询全部成员信息
     * @date: 17:35 2019-3-26
     * @param: currPage 当前页
     * @param: pageSize 每页显示的数量
     * @return: 查询结果以json格斯返回
     */
    @RequestMapping(value = "/getAllMembers", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse getMembers() {

        ResponseBuilder custom = ResponseBuilder.custom();


        //成员信息集合
        List<TeamMember> members = teamMemberService.getAllMember();
        //成员数量
        Long count = teamMemberService.getMemberCount();

        //没有数据
        if (count == 0) {
            custom.failed(Constant.QUERY_FAIL, Constant.ERROR_CODE);
        }

        custom.data(members).success(Constant.SUCCESS, Constant.SUCCESS_CODE);
        return custom.build();
    }

    /**
     * @Description:查询全部成员信息
     * @date: 17:35 2019-3-26
     * @param: currPage 当前页
     * @param: pageSize 每页显示的数量
     * @return: 查询结果以json格斯返回
     */
    @RequestMapping(value = "/admin/getAllMembers", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse adminGetMembers(@RequestParam Integer currPage,
                                        @RequestParam Integer pageSize) {

        ResponseBuilder custom = ResponseBuilder.custom();

        if (Objects.isNull(currPage) || currPage == 0) currPage = 1;
        if (Objects.isNull(pageSize) || pageSize == 0) pageSize = 10;
        //分页
        PageHelper.startPage(currPage, pageSize);

        //成员信息集合
        List<TeamMember> members = teamMemberService.getAllMember();
        //成员数量
        Long count = teamMemberService.getMemberCount();

        //没有数据
        if (count == 0) {
            return custom.failed(Constant.QUERY_FAIL, Constant.ERROR_CODE).build();
        }

        custom.data(members).currPage(currPage).pageSize(pageSize).totalCount(count.intValue());
        return custom.build();
    }

    /**
     * @Description:根据id获取成员信息
     * @date: 17:47 2019-3-26
     * @param: memberId 成员id
     * @return: 成员信息以json格式返回
     */
    @RequestMapping(value = "/getMemberById", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse getMemberById(Integer memberId) {
        TeamMember teamMember;

        ResponseBuilder custom = ResponseBuilder.custom();

        try {
            //查询成员信息
            teamMember = teamMemberService.selectByPrimaryKey(memberId);
        } catch (Exception e) {
            return custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE).build();
        }

        //查询成功
        if (teamMember != null) {
            return custom.data(teamMember).success(Constant.SUCCESS, Constant.SUCCESS_CODE).build();
        }
        //查询失败
        return custom.failed(Constant.FAIL, Constant.ERROR_CODE).build();
    }

    /**
     * @Description:根据id获取成员信息
     * @date: 17:47 2019-3-26
     * @param: memberId 成员id
     * @return: 成员信息以json格式返回
     */
    @RequestMapping(value = "/admin/getMemberById", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse adminGetMemberById(Integer memberId) {
        TeamMember teamMember;

        ResponseBuilder custom = ResponseBuilder.custom();

        try {
            //查询成员信息
            teamMember = teamMemberService.selectByPrimaryKey(memberId);
        } catch (Exception e) {
            return custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE).build();
        }

        //查询成功
        if (teamMember != null) {
            return custom.data(teamMember).success(Constant.SUCCESS, Constant.SUCCESS_CODE).build();
        }
        //查询失败
        return custom.failed(Constant.FAIL, Constant.ERROR_CODE).build();
    }

    /**
     * @Description:添加成员
     * @date: 17:59 2019-3-26
     * @param: teamMember json格式的成员信息
     * @return: 响应信息按json格式返回
     */
    @RequestMapping(value = "/admin/addMember", method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse addMember(@RequestBody TeamMember teamMember) {

        ResponseBuilder custom = ResponseBuilder.custom();

        int result = 0;

        try {
            result = teamMemberService.insertSelective(teamMember);
            custom = Constant.RESULT(result);

        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
            System.out.println(e.toString());
        }

        return custom.build();
    }


    /**
     * 删除团队成员
     *
     * @param memberId 成员id
     * @return
     */
    @RequestMapping(value = "/admin/deleteMember", method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse deleteMemberById(Integer memberId) {

        ResponseBuilder custom = ResponseBuilder.custom();
        int result = 0;

        try {
            result = teamMemberService.deleteByPrimaryKey(memberId);

            custom = Constant.RESULT(result);

        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }

        return custom.build();
    }

    /**
     * 更新成员信息
     *
     * @param teamMember json格式的teamMember实体
     * @return
     */
    @RequestMapping(value = "/admin/updateMember", method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse updateMember(@RequestBody TeamMember teamMember) {
        int result = 0;

        ResponseBuilder custom = ResponseBuilder.custom();

        try {
            result = teamMemberService.updateByPrimaryKeySelective(teamMember);

            custom = Constant.RESULT(result);

        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }
        return custom.build();

    }

}
